package cn.taobaoke.restful.controller.admin;

import cn.taobaoke.restful.controller.BaseController;
import cn.taobaoke.restful.domain.OrderEntity;
import cn.taobaoke.restful.interceptor.Auth;
import cn.taobaoke.restful.service.OrderService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author yangzuo
 * @since 2017-03-25
 */
@RestController
@RequestMapping("/{domain}/{domainId}/api/admins")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderEndpoint extends BaseController {

    @GetMapping("/orders")
    @Auth(type = Auth.AuthType.ADMIN)
    public Result getOrders(@PathVariable Long domainId, @PathVariable String domain, String keyword, Integer page, Integer size) {
        logger.debug("管理员查询订单操作: GET /{}/api/admins/orders?keywords={}&page={}&size={}", domainId, keyword, page, size);
        Pageable pageable = createPageable(page, size, "createTime", "desc");

        Page<OrderEntity> pages = orderService.searchOrderPagingForAdmin(keyword, pageable, domainId, domain);
        return wrapPage(pageable, pages);
    }

    private final @NonNull
    OrderService orderService;

    private static Logger logger = LogManager.getLogger(OrderEndpoint.class);
}
